﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Practice.IService;
using Practice.PH4_2104A.Shopping.SevenLayerFramework.Domin;

namespace Practice.API.Controllers.product
{
    /// <summary>
    /// 分类控制器
    /// </summary>
    [Route("category")]
    [ApiController]
    public class CategoryController : ControllerBase
    {
        ICategoryService _service;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="service"></param>
        public CategoryController(ICategoryService service)
        {
            _service = service;
        }

        /// <summary>
        /// 添加分类
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Insert(CategoryModel model)
        {
            //验证参数
            if (string.IsNullOrEmpty(model.Name))
            {
                return BadRequest(new { code = -600, message = "参数无效" });
            }
            //调用服务层方法
            _service.Insert(model);

            //统一返回值（没有定义DTO，暂时用匿名类替代）
            return Ok(new { code = 200, message = "操作成功" });
        }

        /// <summary>
        /// 分类递归数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRecurse()
        {
            var data = _service.GetRecurseCategory();

            return Ok(new { code = 200, message = "操作成功", data = data });
        }
    }
}
